/*******************************************************************************
 * Copyright (c) 2004, 2009 Blue Bamboo International Inc. 
 * All rights reserved.
 *
 * Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *******************************************************************************/

package com.bambamboo.st.serial.server;

import org.apache.mina.transport.serial.SerialAddress;
import org.apache.mina.transport.serial.SerialAddress.DataBits;
import org.apache.mina.transport.serial.SerialAddress.FlowControl;
import org.apache.mina.transport.serial.SerialAddress.Parity;
import org.apache.mina.transport.serial.SerialAddress.StopBits;

import com.bambamboo.st.ConnectionBean;

/**
 * DOCME
 * 
 * @author Nick.Tan
 * @since 2.0.0
 */
public class SerialConnectionBean extends ConnectionBean {

	private int bauds = 115200;
	private String dataBits;
	private String stopBits;
	private String parity;
	private String flowcontrol;

	public SerialConnectionBean() {
		super();
	}

	public int getBauds() {
		return bauds;
	}

	public void setBauds(int bauds) {
		this.bauds = bauds;
	}

	public String getDataBits() {
		return dataBits;
	}

	public void setDataBits(String dataBits) {
		this.dataBits = dataBits;
	}

	public String getStopBits() {
		return stopBits;
	}

	public void setStopBits(String stopBits) {
		this.stopBits = stopBits;
	}

	public String getParity() {
		return parity;
	}

	public void setParity(String parity) {
		this.parity = parity;
	}

	public String getFlowcontrol() {
		return flowcontrol;
	}

	public void setFlowcontrol(String flowcontrol) {
		this.flowcontrol = flowcontrol;
	}

	@Override
	public String toString() {
		return "SerialConnectionBean [bauds=" + bauds + ", dataBits=" + dataBits + ", flowcontrol=" + flowcontrol
				+ ", parity=" + parity + ", stopBits=" + stopBits + "]";
	}

	public SerialAddress toSerialAddress() {
		DataBits db = toDataBits();
		StopBits sb = toStopBits();
		Parity p = toParity();
		FlowControl fc = toFlowControl();

		return new SerialAddress(getName(), getBauds(), db, sb, p, fc);
	}

	public DataBits toDataBits() {
		DataBits db = DataBits.DATABITS_8;
		try {
			db = DataBits.valueOf(dataBits);
		}
		catch (Exception e) {
		}
		return db;
	}

	public StopBits toStopBits() {
		StopBits sb = StopBits.BITS_1;
		try {
			sb = StopBits.valueOf(stopBits);
		}
		catch (Exception e) {
		}
		return sb;
	}

	public Parity toParity() {
		Parity p = Parity.NONE;
		try {
			p = Parity.valueOf(parity);
		}
		catch (Exception e) {
		}
		return p;
	}

	public FlowControl toFlowControl() {
		FlowControl fc = FlowControl.NONE;
		try {
			fc = FlowControl.valueOf(flowcontrol);
		}
		catch (Exception e) {
		}
		return fc;
	}
}
